package com.test;

import android.app.DownloadManager;
import android.os.AsyncTask;
import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/10/8.
 */

public class DownloadTask  extends AsyncTask<String,Integer,Integer>{

    public static final int TYPE_SUCCESS = 0;
    public static final int TYPE_FAILED= 1;
    public static final int TYPE_PAUSED = 2;
    public static final int TYPE_CACELED = 3;

    private DownloadListener listener;
    private boolean isCanceled = false;
    private boolean isPaused = false;
    private int lastProgress;

    public DownloadTask(DownloadListener listener) {
        this.listener = listener;
    }

    @Override
    protected Integer doInBackground(String... params) {

        InputStream is = null;
        RandomAccessFile savedFile = null;
        File file = null;

        try{

            long downloadedLength = 0;
            String dowloadUrl = params[0];
            String fileName =dowloadUrl.substring(dowloadUrl.lastIndexOf("/"));
            String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();

            file = new File(directory + fileName);
            if(file.exists()){

                downloadedLength = file.length();


            }

            long contentLength = getContentLength(dowloadUrl);
            if(contentLength == 0){

                return TYPE_FAILED;
            }else if(contentLength == downloadedLength){

                return TYPE_SUCCESS;
            }
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().addHeader("RANGE","bytes="+downloadedLength+"-").url(dowloadUrl).build();
            Response response =client.newCall(request).execute();
            if(response != null){

                is = response.body().byteStream();
                savedFile = new RandomAccessFile(file,"rw");
                savedFile.seek(downloadedLength);
                byte[] b = new byte[1024];
                int total =0;
                int len;
                while((len=is.read(b)) != -1){

                    if(isCanceled){

                        return TYPE_CACELED;
                    }else if(isPaused){

                        return TYPE_PAUSED;
                    }else{

                        total += len;
                        savedFile.write(b,0,len);
                        int progress =(int)((total + downloadedLength)*100/contentLength);
                        publishProgress(progress);
                    }
                }

                response.body().close();
                return TYPE_SUCCESS;
            }


        }catch (Exception e){

            e.printStackTrace();
        }finally {
            try{

                if(is != null){

                    is.close();
                }
                if(savedFile != null){

                    savedFile.close();
                }
                if(isCanceled && file != null){

                    file.delete();
                }
            }catch (Exception e){

                e.printStackTrace();
            }
        }


        return TYPE_FAILED;



    }


    @Override
    protected void onPostExecute(Integer integer) {

        switch (integer){

            case TYPE_SUCCESS:
                 listener.onSuccess();
                break;

            case  TYPE_FAILED:
                 listener.onFailed();
                break;

            case  TYPE_PAUSED:

                listener.onPuase();
                break;

            case TYPE_CACELED:

                listener.onCanceled();
                break;
            default:

                break;



        }


    }

    @Override
    protected void onProgressUpdate(Integer... values) {

        int progress = values[0];
        if(progress > lastProgress){

            listener.onProgress(progress);
            lastProgress=progress;
        }//how to handle if deep big 100


    }

    public void  pauseDownload(){

        isPaused = true;

    }

    public void  cancleDownload(){

        isCanceled = true;
    }

    private long  getContentLength(String downloadUrl) throws IOException{


        OkHttpClient client= new OkHttpClient();
        Request request = new Request.Builder().url(downloadUrl).build();
        Response response = client.newCall(request).execute();
        if( response != null && response.isSuccessful()){

            long contentLength = response.body().contentLength();
            response.close();
            return contentLength;
        }


        return 0;
    }
}
